<?php
require_once './vendor/autoload.php';

use \Elasticsearch\ClientBuilder;

class Demo
{
    public $client;

    public function __construct()
    {
        $hosts = array('127.0.0.1:9200');
        $this->client = ClientBuilder::create()->setHosts($hosts)->build();

    }

    public function install()
    {
        $params = [
            'index' => 'tp_api', //相当于 mysql的数据名称
            'type' => 'wechat_article', //相当于mysql 表名字
            'id' => 'my_id', //相当于mysql 表字段id
            'body' => ['name' => '刘强今天很高兴'] // mysql 一列内容数据
        ];
        // 添加数据到elasticsearch
        $this->client->index($params);
    }

    public function select()
    {

//        $params = [
//            'index' => 'tp_api', //相当于 mysql的数据名称
//            'type' => 'wechat_article', //相当于mysql 表名字
//            'id' => 'my_id', //相当于mysql 表字段id
//        ];
//        //精确查找
//        $this->client->get($params);

        // 模糊匹配查询
//        $params = [
//            'index' => 'tp_api',
//            'type' => 'wechat_article',
//            'body' => [
//                'query' => [ // 查询时必须制定的`query`参数
//                    'match' => [
//                        'title' => '夫妻'
//                    ]
//                ]
//            ]
//        ];
//        $res = $this->client->search($params);


        //分页查询
        $params = [
            'index' => 'tp_api',
            'type' => 'wechat_article',
            'body' => [
                'query' => [ // 查询时必须制定的`query`参数
                    'bool' => [
//                        'filter' => [
//                            'term' => [ 'name' => '克勤阿胶4.9-2' ]
//                        ],
                        'should' => [
                            'match' => ['title' => '夫妻']
                        ]
                    ]
                ]
            ],
            'size' => 2, //得到两条数据
            'from' => 1  //多少条数据开始放弃
        ];
//        $json = '{"query":{"bool":{"filter":[{"term":{"age":28}}]}},"size":20,"from":0,"sort":[{"timestamp":{"order":"desc"},"_id":{"order":"desc"}}]}';
//
//        $a = '{"index":"tp_api","type":"wechat_article","body":{"bool":{"filter":{"term":{"title":"\u592b\u59bb"}}}}}';
//        var_dump($json);
//        var_dump(json_encode($params['body']));
//        exit();
        $res = $this->client->search($params);
        var_dump($res);
    }

    public function del()
    {
        $params = [
            'index' => 'tp_api', //相当于 mysql的数据名称
            'type' => 'wechat_article', //相当于mysql 表名字
            'id' => 'my_id', //相当于mysql 删除一条数据
        ];
        $this->client->delete($params);
    }

    public function update()
    {
        $params = [
            'index' => 'tp_api', //相当于 mysql的数据名称
            'type' => 'wechat_article', //相当于mysql 表名字
            'id' => 'my_id', //相当于mysql 表字段id
            'body' => [
                'name' => '刘强今天很高兴',
                'ages' => 12
            ]
        ];
        $this->client->update($params);
    }

}



